package cn.tth.test.io;

import com.github.shyiko.mysql.binlog.BinaryLogFileReader;
import com.github.shyiko.mysql.binlog.event.Event;
import com.github.shyiko.mysql.binlog.event.EventData;
import com.github.shyiko.mysql.binlog.event.RowsQueryEventData;
import com.github.shyiko.mysql.binlog.event.deserialization.EventDeserializer;
import lombok.SneakyThrows;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.*;

public class Thrand {



    public static void main(String[] args){
        threadOom();
    }

    /**
     * 线程溢出
     */
    public static void threadOom() {
        for (int i = 0; ; i++) {
            System.out.println("i = " + i);
            new Thread(new HoldThread()).start();
        }
    }

    static class HoldThread extends Thread {
        CountDownLatch cdl = new CountDownLatch(1);

        public HoldThread() {
            this.setDaemon(true);
        }

        public void run() {
            try {
                cdl.await();
            } catch (InterruptedException e) {
                System.out.println(e.getMessage());
            }
        }
    }

    @SneakyThrows
    public static List<String> run(){
        ExecutorService fixedThreadPool = Executors.newFixedThreadPool(10);

        Future<String> future = fixedThreadPool.submit(new Runnable() {
            @SneakyThrows
            @Override
            public void run() {
                try {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }finally {
                        unlock();
                    System.out.println("解锁");
                }
            }
        },"YES");

        Future<String> future1 = fixedThreadPool.submit(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"YES@");

        List<Future<String>> futureList = new ArrayList();
        futureList.add(future);
        futureList.add(future1);

        List<String> stringList = new ArrayList<>();
        futureList.forEach(o -> {
            try {
                stringList.add(o.get());
            }catch (ExecutionException | InterruptedException e){
                System.out.println(e.getMessage());
            }
        });
        return stringList;
    }

    private static void unlock() throws ExecutionException, InterruptedException {
        ExecutorService fixedThreadPool1 = Executors.newFixedThreadPool(1);
        try {
            fixedThreadPool1.submit(new Runnable() {
                @SneakyThrows
                @Override
                public void run() {
                    Thread.sleep(2000);
                    int i = 1/0;
                }
            }).get();
        }catch (Exception e){
            throw e;
//            System.out.println("unlock");
        }
    }

}
